﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CStock;
using NPlot;
using CTrading;
using CUtil;

using System.Collections;
using System.Diagnostics;

namespace CGraph
{
    public class ChartControlsHelper
    {
        public static void Test_CandlePlot(ChartControl myChartControl, System.Data.DataTable dt)
        {

            NPlot.Windows.PlotSurface2D plotSurface = myChartControl.PlotSurface;
            float penWidth = myChartControl.PenWidth;

            //------------------------------
            plotSurface.Clear();


            DataView dv = new DataView(dt);

            // create CandlePlot.
            CandlePlot cp = new CandlePlot();
            cp.DataSource = dt;
            cp.AbscissaData = "Date";
            cp.OpenData = "Open";
            cp.LowData = "Low";
            cp.HighData = "High";
            cp.CloseData = "Close";
            cp.BearishColor = Color.Red;
            cp.BullishColor = Color.Green;
            cp.Style = NPlot.CandlePlot.Styles.Filled;

            // calculate 10 day moving average and 2*sd line
            ArrayList av10 = new ArrayList();
            ArrayList sd2_10 = new ArrayList();
            ArrayList sd_2_10 = new ArrayList();
            ArrayList dates = new ArrayList();
            for (int i = 0; i < dt.Rows.Count - 10; ++i)
            {
                double sum = 0.0f;
                for (int j = 0; j < 10; ++j)
                {
                    sum += (double)dt.Rows[i + j]["Close"];
                }
                double average = sum / 10.0f;
                av10.Add(average);
                sum = 0.0f;
                for (int j = 0; j < 10; ++j)
                {
                    sum += ((double)dt.Rows[i + j]["Close"] - average) * ((double)dt.Rows[i + j]["Close"] - average);
                }
                sum /= 10.0f;
                sum = 2.0f * (double)Math.Sqrt(sum);
                sd2_10.Add(average + sum);
                sd_2_10.Add(average - sum);
                dates.Add((DateTime)dt.Rows[i + 10]["Date"]);
            }

            // and a line plot of close values.
            LinePlot av = new LinePlot();
            av.OrdinateData = av10;
            av.AbscissaData = dates;
            av.Color = Color.LightGray;
            av.Pen.Width = penWidth;

            LinePlot top = new LinePlot();
            top.OrdinateData = sd2_10;
            top.AbscissaData = dates;
            top.Color = Color.LightSteelBlue;
            top.Pen.Width = penWidth;

            LinePlot bottom = new LinePlot();
            bottom.OrdinateData = sd_2_10;
            bottom.AbscissaData = dates;
            bottom.Color = Color.LightSteelBlue;
            bottom.Pen.Width = penWidth;

            FilledRegion fr = new FilledRegion(top, bottom);
            //fr.RectangleBrush = new RectangleBrushes.Vertical( Color.FloralWhite, Color.GhostWhite );
            fr.RectangleBrush = new RectangleBrushes.Vertical(Color.FromArgb(255, 255, 240), Color.FromArgb(240, 255, 255));
            plotSurface.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            plotSurface.Add(fr);

            plotSurface.Add(new Grid());

            plotSurface.Add(av);
            plotSurface.Add(top);
            plotSurface.Add(bottom);
            plotSurface.Add(cp);

            if (dt.Rows.Count > 59)
            {
                // now make an arrow... 
                ArrowItem arrow = new ArrowItem(new PointD(((DateTime)dt.Rows[60]["Date"]).Ticks, 2.28), -80, "An interesting flat bit");
                arrow.ArrowColor = Color.DarkBlue;
                arrow.PhysicalLength = 50;
                plotSurface.Add(arrow);

            }

            plotSurface.Title = "AU:JBH";
            //plotSurface.XAxis1.Label = "Date / Time";
            plotSurface.XAxis1.WorldMin += plotSurface.XAxis1.WorldLength / 4.0;
            plotSurface.XAxis1.WorldMax -= plotSurface.XAxis1.WorldLength / 2.0;
            //plotSurface.YAxis1.Label = "Price [$]";

            plotSurface.XAxis1 = new TradingDateTimeAxis(plotSurface.XAxis1);

           
            plotSurface.PlotBackColor = Color.White;
            plotSurface.BackColor = Color.White;
            plotSurface.XAxis1.Color = Color.Black;
            plotSurface.YAxis1.Color = Color.Black;
            myChartControl.PlotLegend();
            plotSurface.Refresh();

            myChartControl.AddAllInteractions();

           

        }
        #region only testing

        public static void Test_LinePlot(ChartControl myChartControl,
            System.Collections.ArrayList xAxisCollection, System.Collections.ArrayList yAxisCollection)
        {
            NPlot.Windows.PlotSurface2D plotSurface = myChartControl.PlotSurface;
            float penWidth = myChartControl.PenWidth;
            // and a line plot of close values.
            LinePlot av = new LinePlot();
            av.OrdinateData = yAxisCollection;
            av.AbscissaData = xAxisCollection;
            av.Color = Color.LightGray;
            av.Pen.Width = penWidth;

            plotSurface.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            plotSurface.Add(new Grid());
            plotSurface.Add(av);



            plotSurface.Title = "AU:JBH";
            //plotSurface.XAxis1.Label = "Date / Time";
            plotSurface.XAxis1.WorldMin += plotSurface.XAxis1.WorldLength / 4.0;
            plotSurface.XAxis1.WorldMax -= plotSurface.XAxis1.WorldLength / 2.0;
            //plotSurface.YAxis1.Label = "Price [$]";

          
            // make sure plot surface colors are as we expect - the wave example changes them.
            plotSurface.PlotBackColor = Color.White;
            plotSurface.BackColor = Color.White;
            plotSurface.XAxis1.Color = Color.Black;
            plotSurface.YAxis1.Color = Color.Black;
            plotSurface.Refresh();
            plotSurface.Add(av);
            myChartControl.PlotLegend();
            plotSurface.Refresh();

            
        }


        public static void Test_PlotThreeCharts(ChartControl myChartControl, DataTable dt)
        {
            NPlot.Windows.PlotSurface2D plotSurface = myChartControl.PlotSurface;
            float penWidth = myChartControl.PenWidth;

            plotSurface.Clear();
            plotSurface.DateTimeToolTip = true;

            // obtain stock information from xml file
            DataSet ds = new DataSet();

            DataView dv = new DataView(dt);

            // create CandlePlot.
            CandlePlot cp = new CandlePlot();
            cp.DataSource = dt;
            cp.AbscissaData = "Date";
            cp.OpenData = "Open";
            cp.LowData = "Low";
            cp.HighData = "High";
            cp.CloseData = "Close";
            cp.BearishColor = Color.Red;
            cp.BullishColor = Color.Green;
            cp.Style = NPlot.CandlePlot.Styles.Filled;

            // calculate 10 day moving average and 2*sd line
            ArrayList av10 = new ArrayList();
            ArrayList sd2_10 = new ArrayList();
            ArrayList sd_2_10 = new ArrayList();
            ArrayList dates = new ArrayList();
            for (int i = 0; i < dt.Rows.Count - 10; ++i)
            {
                double sum = 0.0f;
                for (int j = 0; j < 10; ++j)
                {
                    sum += (double)dt.Rows[i + j]["Close"];
                }
                double average = sum / 10.0f;
                av10.Add(average);
                sum = 0.0f;
                for (int j = 0; j < 10; ++j)
                {
                    sum += ((double)dt.Rows[i + j]["Close"] - average) * ((double)dt.Rows[i + j]["Close"] - average);
                }
                sum /= 10.0f;
                sum = 2.0f * (double)Math.Sqrt(sum);
                sd2_10.Add(average + sum);
                sd_2_10.Add(average - sum);
                dates.Add((DateTime)dt.Rows[i + 10]["Date"]);
            }

            // and a line plot of close values.
            LinePlot av = new LinePlot();
            av.OrdinateData = av10;
            av.AbscissaData = dates;
            av.Color = Color.LightGray;
            av.Pen.Width = penWidth;

            LinePlot top = new LinePlot();
            top.OrdinateData = sd2_10;
            top.AbscissaData = dates;
            top.Color = Color.LightSteelBlue;
            top.Pen.Width = penWidth;

            LinePlot bottom = new LinePlot();
            bottom.OrdinateData = sd_2_10;
            bottom.AbscissaData = dates;
            bottom.Color = Color.LightSteelBlue;
            bottom.Pen.Width = penWidth;

            FilledRegion fr = new FilledRegion(top, bottom);
            //fr.RectangleBrush = new RectangleBrushes.Vertical( Color.FloralWhite, Color.GhostWhite );
            fr.RectangleBrush = new RectangleBrushes.Vertical(Color.FromArgb(255, 255, 240), Color.FromArgb(240, 255, 255));
            plotSurface.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            plotSurface.Add(fr);

            plotSurface.Add(new Grid());

            plotSurface.Add(av);
            plotSurface.Add(top);
            plotSurface.Add(bottom);
            plotSurface.Add(cp);

            // now make an arrow... 
            ArrowItem arrow = new ArrowItem(new PointD(((DateTime)dt.Rows[60]["Date"]).Ticks, 2.28), -80, "An interesting flat bit");
            arrow.ArrowColor = Color.DarkBlue;
            arrow.PhysicalLength = 50;

            plotSurface.Add(arrow);

            plotSurface.Title = "AU:JBH";
            //plotSurface.XAxis1.Label = "Date / Time";
            //plotSurface.YAxis1.Label = "Price [$]";

            myChartControl.PlotLegend();
            plotSurface.Refresh();

        }

        #endregion

    }
}
